package org.giovanolli.mumi.controller.administration;

import java.util.HashSet;

import org.giovanolli.mumi.controller.AbstractController;
import org.giovanolli.mumi.controller.AbstractModel;
import org.giovanolli.mumi.entities.filter.UserFilter;
import org.giovanolli.mumi.entities.security.Role;
import org.giovanolli.mumi.entities.security.User;
import org.giovanolli.mumi.service.MailService;
import org.giovanolli.mumi.service.Service;
import org.giovanolli.mumi.service.UserService;
import org.giovanolli.mumi.view.datamodel.DefaultSelectionDataModelListener;
import org.giovanolli.mumi.view.datamodel.SelectableDataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

@Controller("userController")
public class UserController extends AbstractController<User> {

	@Autowired
	private UserService service;

	@Autowired
	private UserModel model;

	@Autowired
	private MailService<User> mailConfirmationService;

	public void init() {
		super.init();
		model.setFilter(new UserFilter());
	}

	@Override
	protected void bindModelToEntity() {
		User editedUser = model.getEditedEntity();
		editedUser.setFirstName(model.getFirstName());
		editedUser.setLastName(model.getLastName());
		editedUser.setEmail(model.getEmail());
		editedUser.setUsername(model.getUsername());
		if (model.isNewMode()) {
			editedUser.setPassword(model.getPassword());
		}

		editedUser.getRoles().clear();
		editedUser.getRoles().addAll(model.getRolesSelectionListener().getSelectedElements());
	}

	@Override
	protected void bindEntityToModel() {
		User editedUser = model.getEditedEntity();

		model.setFirstName(editedUser.getFirstName());
		model.setLastName(editedUser.getLastName());
		model.setEmail(editedUser.getEmail());
		model.setUsername(editedUser.getUsername());
		model.setPassword(editedUser.getPassword());
		DefaultSelectionDataModelListener<Role> selectionListener = new DefaultSelectionDataModelListener<Role>(new HashSet<Role>(editedUser.getRoles()));
		SelectableDataModel<Role> permissionDataModel = new SelectableDataModel<Role>(service.getAllRoles(), selectionListener);
		model.setRolesDataModel(permissionDataModel);
		model.setRolesSelectionListener(selectionListener);
	}

	@Override
	@Transactional(readOnly = true)
	protected void save() {
		User editedUser = model.getEditedEntity();
		try {
			mailConfirmationService.sendEmail(editedUser);
			service.saveUser(editedUser, model.isNewMode());
		} catch (MailException e) {
			e.printStackTrace();
		}

	}

	public void prepareResetPassword() {
		model.setUsername(model.getEditedEntity().getUsername());
		model.setPassword(null);
		model.setConfirmPassword(null);
	}

	@Transactional(readOnly = true)
	public void confirmResetPassword() {
		User editedUser = model.getEditedEntity();
		editedUser.setPassword(model.getPassword());
		service.saveUser(editedUser, true);
	}

	@Override
	protected AbstractModel<User> getModel() {
		return model;
	}

	@Override
	protected Service<User> getService() {
		return service;
	}

	protected Class<User> getEntityClass() {
		return User.class;
	}
}
